package com.ssy.file.service.controller;


import com.ssy.file.service.config.ServerTypeConfig;
import com.ssy.file.service.service.FileUploadService;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.dto.response.FileItemResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "文件管理")
@RestController
@RequestMapping("/file")
public class FileUploadController extends BaseController {

    @Autowired
    ServerTypeConfig serverTypeConfig;

    @Resource
    FileUploadService fileUploadService;

    /**
     * 上传文件没有限制文件类型
     * serverType 文件上传类型:1.阿里云oss 2.fastdfs
     * @author wrc
     * @date 2020/5/29
     */
    @ApiOperation(value = " 文件上传")
    @PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Wrapper<String> upload(@RequestPart("file") MultipartFile file) {
        Integer serverType = serverTypeConfig.getServerType();
        return fileUploadService.upload(file, serverType);
    }

    /**
     * 上传文件没有限制文件类型
     * serverType 文件上传类型:1.阿里云oss 2.fastdfs
     * @author wrc
     * @date 2020/5/29
     */
    @ApiOperation(value = " 文件上传")
    @PostMapping(value = "/upload/prefix",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Wrapper<FileItemResponse> uploadPrefix(@RequestPart("file") MultipartFile file, @RequestParam(value = "prefix") String prefix) {
        Integer serverType = serverTypeConfig.getServerType();
        return fileUploadService.uploadPrefix(file, serverType, prefix);
    }


    /**
     * 删除文件-根据文件访问url删除
     * serverType 文件上传类型:1.阿里云oss 2.fastdfs
     * @author wrc
     * @date 2020/5/29
     */
    @ApiOperation(value = " 删除文件")
    @PostMapping(value = "/delete",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Wrapper<Void> delete(@RequestPart(value = "fileUrl") String fileUrl) {
        Integer serverType = serverTypeConfig.getServerType();
        return fileUploadService.delete(fileUrl, serverType);
    }

    /**
     * 上传文件没有限制文件类型
     * serverType 文件上传类型:1.阿里云oss 2.fastdfs
     * @author wrc
     * @date 2020/5/29
     */
    @ApiOperation(value = " 文件上传批量")
    @PostMapping(value = "/upload/batch",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Wrapper<List<FileItemResponse>> uploadBatch(HttpServletRequest request, @RequestParam(value = "prefix",required = false) String prefix) {
        Integer serverType = serverTypeConfig.getServerType();
        return fileUploadService.uploadBatch(request, serverType, prefix);
    }
}
